package com.ebeijia.web.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebeijia.mybatis.pojo.NoticeInfo;
import com.ebeijia.service.user.NoticeService;
import com.ebeijia.util.DateUtil;
import com.ebeijia.web.ResponseMessage;

@Controller
@RequestMapping("/notice")
public class NoticeController {

	@Autowired
	private NoticeService noticeService;

	/**
	 * 查询所有公告
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryNoticeList")
	@ResponseBody
	public ResponseMessage queryNoticeList(String busType,
			HttpServletRequest request) throws Exception {
		List<NoticeInfo> noticeInfos = noticeService.queryNoticeList(busType);
		if (noticeInfos != null && noticeInfos.size() != 0) {
			for (NoticeInfo noticeInfo : noticeInfos) {
				String deployDate = DateUtil.formatDate(
						noticeInfo.getNoticeDepDate(), "yyyy.MM.dd");
				int index = deployDate.indexOf(".");
				String year = deployDate.substring(0, index) + "年";
				noticeInfo.setDeployDate(deployDate.substring(index + 1));
				noticeInfo.setYearIndex(year);
			}
			return ResponseMessage.success(noticeInfos);
		} else {
			return ResponseMessage.error("0002", "没有通告消息！");
		}
	}

	@RequestMapping("/queryNoticeInfo")
	@ResponseBody
	public ResponseMessage queryNoticeInfo(String noticeId,
			HttpServletRequest request) throws Exception {

		if (StringUtils.isEmpty(noticeId)) {
			return ResponseMessage.error("无此信息");
		}

		return noticeService.queryNoticeInfoById(noticeId);
	}

}
